add_definitions(-DUNICODE)

# Instructs the MSVC toolset to use the precompiled header PRECOMPILED_HEADER
# for each source file given in the collection named by SOURCE_VARIABLE_NAME.
function(enable_precompiled_headers PRECOMPILED_HEADER SOURCE_VARIABLE_NAME)
  if(MSVC)
    set(files ${${SOURCE_VARIABLE_NAME}})

    # Generate precompiled header translation unit
    get_filename_component(pch_basename ${PRECOMPILED_HEADER} NAME_WE)
    set(pch_abs ${CMAKE_CURRENT_SOURCE_DIR}/${PRECOMPILED_HEADER})
    set(pch_unity ${CMAKE_CURRENT_BINARY_DIR}/${pch_basename}.cpp)
    FILE(WRITE ${pch_unity} "// Precompiled header unity generated by CMake\n")
    FILE(APPEND ${pch_unity} "#include <${pch_abs}>\n")
    set_source_files_properties(${pch_unity}  PROPERTIES COMPILE_FLAGS "/Yc\"${pch_abs}\"")

    # Update properties of source files to use the precompiled header.
    # Additionally, force the inclusion of the precompiled header at beginning of each source file.
    foreach(source_file ${files} )
      set_source_files_properties(
        ${source_file}
        PROPERTIES COMPILE_FLAGS
        "/Yu\"${pch_abs}\" /FI\"${pch_abs}\""
      )
    endforeach(source_file)

    # Finally, update the source file collection to contain the precompiled header translation unit
    set(${SOURCE_VARIABLE_NAME} ${pch_unity} ${${SOURCE_VARIABLE_NAME}} PARENT_SCOPE)
  endif(MSVC)
endfunction(enable_precompiled_headers)

set(MATH_SRC_FILES
        vector4.h
        vector4.inl
        vector4.cpp
        matrix4x4.h
        matrix4x4.inl
        matrix4x4.cpp
        quaternion.h
        quaternion.inl
        quaternion.cpp
        )

set(SCENE_SRC_FILES
        scene_node.h
        scene_node.cpp
        scene.h
        scene.cpp
        )

set(RENDER_SRC_FILES
        render_device.h
        render_device.cpp
        vertex_index_buffer.h
        vertex_index_buffer.cpp
        texture.h
        texture.cpp
        gpu_program.h
        gpu_program.cpp
        mesh.h
        mesh.cpp
        material.h
        material.cpp
        resource.h
        resource.cpp
        entity.h
        entity.cpp
        camera.h
        camera.cpp
        )

set(UNIVERSE_SRC_FILES
        pch.h
        debug_util.h
        prerequisites.h
        singleton.h
        )

set(APP_SRC_FILES
        scene_manager_demo.cpp)

set(SRC_FILES 
        ${MATH_SRC_FILES} 
        ${SCENE_SRC_FILES}
        ${RENDER_SRC_FILES}
        ${UNIVERSE_SRC_FILES}
        ${APP_SRC_FILES}
        )

source_group("math" FILES ${MATH_SRC_FILES})
source_group("scene" FILES ${SCENE_SRC_FILES})
source_group("render" FILES ${RENDER_SRC_FILES})
source_group("resource" FILES ${RESOURCE_SRC_FILES})
source_group("universe" FILES ${UNIVERSE_SRC_FILES})
source_group("app" FILES ${APP_SRC_FILES})

enable_precompiled_headers(pch.h ${SRC_FILES}) 

include_directories(${DIRECTX_INCLUDE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/deps/include/DXUT)
link_directories(${DIRECTX_LIBRARY_DIR})
link_directories(${PROJECT_SOURCE_DIR}/deps/lib/debug)
link_directories(${PROJECT_SOURCE_DIR}/deps/lib/release)

add_executable(${TARGET} WIN32 ${SRC_FILES})
target_link_libraries(${TARGET} 
        debug d3d9.lib d3dx9d.lib d3d10.lib d3dx10d.lib dxguid.lib dxerr.lib dxut_d.lib winmm.lib comctl32.lib dxutopt_d.lib 
        optimized d3d9.lib d3dx9.lib d3d10.lib d3dx10.lib dxguid.lib dxerr.lib winmm.lib comctl32.lib dxut.lib dxutopt.lib)

install(TARGETS ${TARGET} 
        RUNTIME DESTINATION bin)
